package com.rem.concurrency.juc.my13Single;

/**
 * DCL 懒汉式
 * 创建一个线程不安全的懒汉式单例
 *
 * @author Rem
 * @date 2021-03-10
 */

public class LazyMan {

    private LazyMan() {
        System.out.println(Thread.currentThread().getName() + "ok");
    }

    private static LazyMan lazyMan;


    /**
     * 再需要使用的时候才创建对象 但是会有线程安全问题
     *
     * @return
     */
    public static LazyMan getLazyMan() {
        if (lazyMan == null) {
            //不是一个原子性的操作
            lazyMan = new LazyMan();
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                LazyMan lazyMan = LazyMan.getLazyMan();
                System.out.println(lazyMan);
            }).start();
        }
    }
}